package com.example.online_order.conrtoller;


import com.example.online_order.pojo.Orders;
import com.example.online_order.pojo.PageItem;
import com.example.online_order.pojo.Result;
import com.example.online_order.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {

//    查询（订单号查询，模糊查询：用户名查询，菜品查询；全表查询） 新增 删除（客户删除只能让客户自己看不见，不实际删除记录 商家可以删除记录），不支持修改
//    动态sql查询：商品（类型，名称），购物车：（类型，商品），订单（用户名，订单号，商品名）


    @Autowired
    OrdersService ordersService;

    @GetMapping("/one/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Orders orderDb = ordersService.findById(id);
        if (orderDb == null){
            return Result.fail("该订单不存在",404);
        }else {
            return Result.success(orderDb);
        }
    }


    @GetMapping("/byuser/{userName}")
        public Result findByUserName(@PathVariable("userName") String userName){
        List<Orders> orderDb = ordersService.findByUserName(userName);
        if (orderDb == null){
            return Result.fail("该用户的订单不存在",404);
        } else {
            return Result.success(orderDb);
        }
    }


    @GetMapping("/byorder/{orderNum}")
    public Result findByOrderNum(@PathVariable ("orderNum") String orderNum){
        Orders orderDb = ordersService.findByOrderNum(orderNum);
        if (orderDb == null){
            return Result.fail("该订单不存在",404);
        } else {
            return Result.success(orderDb);
        }
    }


    @GetMapping("/like/userName/{userName}/goodsName/{goodsName}")
    public Result findLikeUserNameAndProduct(@PathVariable("userName") String userName, @PathVariable("goodsName") String goodsName){
        List<Orders> orderDb = ordersService.findLikeUserNameAndProduct(userName, goodsName);
        if (orderDb.isEmpty()){
            return Result.fail("该查询结果为空",404);
        } else {
            return Result.success(orderDb);
        }
    }


    @GetMapping("/getAll")
    public Result findAll(){
        List<Orders> orderDb = ordersService.findAll();
        if (orderDb.isEmpty()){
            return Result.fail("该查询结果为空",404);
        } else {
            return Result.success(orderDb);
        }
    }


    @PostMapping("/add")
    public Result addOrder(Orders orders){
        Integer res = ordersService.addOrder(orders);
        if (res == 0){
            return Result.fail("添加失败",500);
        } else {
            return Result.success(null);
        }
    }


    @DeleteMapping("/del/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        Integer res = ordersService.deleteById(id);
        if (res == 0){
            return Result.fail("删除失败",500);
        } else {
            return Result.success(null);
        }
    }


    @DeleteMapping("/dels/{ids}")
    public Result deleteByids( @PathVariable ("ids") int[] ids){
        boolean res = ordersService.deleteByids(ids);
        if (!res){
            return Result.fail("删除失败",500);
        } else {
            return Result.success(null);
        }
    }


    @PutMapping("/clientdel/{id}")
    public  Result clientDlite(@PathVariable("id") Integer id){
        Integer res = ordersService.clientDlite(id);
        if (res == 0){
            return Result.fail("删除失败",500);
        } else {
            return Result.success(null);
        }
    }

    @PutMapping("/Clientupdate/orderid/{orderid}")
    public Result clientUpdate( @PathVariable("orderid") Integer orderid){
        Integer res = ordersService.clientUpdate(orderid);
        return Result.success(res);

    }
    @GetMapping("/page/current/{current}/size/{size}/userName/{userName}")
    public Result queryPage(@PathVariable("current") Integer current, @PathVariable("size") Integer size,@PathVariable("userName") String userName ){
        PageItem<Orders> pageItem = ordersService.queryPage(current, size,userName);
        return Result.success(pageItem);
    }

    @GetMapping("/pageAll/current/{current}/size/{size}")
    public Result queryPageAll(@PathVariable("current") Integer current, @PathVariable("size") Integer size){
        PageItem<Orders> pageItem = ordersService.queryPageAll(current, size);
        return Result.success(pageItem);
    }





}
